package com.itheima.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @version 1.0
 * @description 通用响应结果
 * @package com.hmall.vo
 */
@Data
@AllArgsConstructor
public class R<T> implements Serializable {
    private Integer code;
    private String errorMsg;
    private T data;

    public R() {
        this.code = HttpStatus.OK.value();
    }

    public static R ok() {
        return new R();
    }

    public static R okMsg(String msg) {
        return new R(HttpStatus.OK.value(),null,msg);
    }

    public static <T>R<T> okData(T data) {
        return new R(HttpStatus.OK.value(),"操作成功!",data);
    }
    public static R error() {
        return R.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),"发生未知异常！");
    }
    public static R error(int code, String msg) {
        return new R(code,msg,null);
    }
    public static R bizError(String msg) {
        return R.error(9999, msg);
    }

    public boolean isSuccess(){
        return this.code!=null&&this.code.intValue()==HttpStatus.OK.value();
    }
}
